var mongoose=require('mongoose');
var config=require('../config');
var mongodb = require('mongodb');
var Schema=mongoose.Schema;
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    db.close();
    console.log(error);
});

db.on('close', function () {
    db.open(config.db_url);
    console.log("重新连接mongo");
});


var buyerSchema = new Schema({
    buyer_id: String,
    buyer: String, //购买人
    account:String, //用户登陆账号
    password:String, //用户登陆密码
    phoneNumber: String, //购买人联系电话
    company: String, //购买人所属公司
    level: {type:Number,default:0} //购买人等级 
});

var Buyer = db.model("Buyer", buyerSchema);

Buyer.prototype.findBuyerById=function (buyer,callback){
    Buyer.findOne({buyer:buyer}, function(err, datas){
        return callback(datas);
    });
}

Buyer.prototype.findBuyerBy_Id=function (id,callback){
    Buyer.findOne({_id:id}, function(err, datas){
        return callback(datas);
    });
}

Buyer.prototype.removeById=function (buyerid,callback){
    Buyer.remove({buyer_id:buyerid}, function(err){
        return callback(err);
    });
}

Buyer.prototype.updatePwd=function(account,password,callback){
    Buyer.update({account:account},{password:password},function(err){
        return callback(err);
    })
}

Buyer.prototype.findById=function (buyer_id,callback){
    Buyer.findOne({buyer_id:buyer_id}, function(err, datas){
        return callback(datas);
    });
}

Buyer.prototype.findPwdByAcc=function (account,callback){
    Buyer.findOne({account:account}, function(err, datas){
        return callback(err,datas);
    });
}

Buyer.prototype.findAll=function(callback){
    Buyer.find(function(err,datas){
        return callback(datas);
    })
}

Buyer.prototype.findBuyerByBuyer=function (buyer,callback){
    Buyer.findOne({buyer:buyer}, function(err, doc){
        if(doc){
            return callback(err, doc);
        }
        return callback(err);
    });
}

Buyer.prototype.findAccountByAccount=function (username,callback){
    Buyer.findOne({account:username}, function(err, doc){
        if(doc){
            return callback(err, doc);
        }
        return callback(err);
    });
}

//修改buyer数据
Buyer.prototype.updateBuyerById=function(id,buyer,phoneNumber,company,level,callback){
    Buyer.update({_id:id},{buyer:buyer,phoneNumber:phoneNumber,company:company,level:level},function(err){
        return callback(err);
    })
}


Buyer.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Buyer.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Buyer.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}

module.exports = Buyer;